package com.heshi.producthomework.service;

import com.heshi.producthomework.entity.Product;
import com.heshi.producthomework.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductService {
    @Autowired
    ProductMapper productMapper;
    public Integer addProduct(Product product){
        return productMapper.insertProduct(product);
    }

    public List<Product> searchByTitle(String keyword) {
        return productMapper.selectByTitleLike(keyword == null ? "" : keyword);
    }

    public List<Product> listLowStockAggregated() {
        return productMapper.selectLowStockAggregated();
    }

    public List<Product> pageList(int page, int pageSize) {
        int safePage = page <= 0 ? 1 : page;
        int safePageSize = pageSize <= 0 ? 10 : pageSize;
        int offset = (safePage - 1) * safePageSize;
        return productMapper.selectPage(offset, safePageSize);
    }
}
